package com.slwish.xty.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.CreditLifeModel;
import com.slwish.xty.mvp.contract.CreditLifeContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * 首页
 *
 * @author 陈浩
 * @version 1.0.0
 */
public class CreditLifePresenter extends BasePresenter<CreditLifeContract.View> implements CreditLifeContract.Presenter {

    public CreditLifePresenter(CreditLifeContract.View view) {
        // 初始化数据仓库
        super(view);
    }

    /**
     * 登录
     *
     * @param context 上下文
     */
    @Override
    public void getAccommodationInfo(Context context) {
        //处理账号及密码

        EasyHttp.post(UrlConfig.ACCOMMODATION_INFO_URL)
                .execute(new ProgressDialogCallBack<String>(showProgressDialog(context)) {

                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "json:" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String data = jsonObject.getString("data");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
                                return;
                            }
                            List<CreditLifeModel> model = JSON.parseArray(data, CreditLifeModel.class);
                            if (getView() != null) {
                                getView().getInfoSuccess(model);
//                                setError(R.string.login_success_text);
                            }
                        } catch (Exception e) {
                            setError(R.string.loadig_failed_text);
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        setError(R.string.loadig_failed_text);
                    }
                });
    }

}
